package net.sxnic.ugr.role.util;

import java.util.List;
import java.util.Set;

import net.sxnic.ugr.menu.Menu;
import net.sxnic.ugr.role.Role;

import org.apache.commons.lang.StringUtils;

public class RoleUtils {

	public static String createCheckboxHtml(Set<Role> roles,
			List<Role> allRoles, String name) {

		StringBuffer sb = new StringBuffer();

		String temp = ",";
		for (Role r : roles) {
			temp = temp + r.getRole() + ",";
		}
 
		
		int i=0;
        String lstrbr="";
		 
			
		for (Role r : allRoles) {
			i++;
			lstrbr=i%3==0?"<br>":"";
			if (StringUtils.contains(temp, ","+r.getRole()+",")) {
				sb.append("<input type='checkbox' name='" + name + "' value='"
						+ r.getRole() + "' checked='true' />&nbsp;"
						+ r.getName() + "&nbsp;"+lstrbr);
			} else {
				sb.append("<input type='checkbox' name='" + name + "' value='"
						+ r.getRole() + "' />&nbsp;" + r.getName() + "&nbsp;"+lstrbr);
			}
		}

		return sb.toString();

	}
	
	public static String createCheckboxMenuHtml(Set<Menu> menus,
			List<Menu> allMenus, String name) {

		StringBuffer sb = new StringBuffer();

		String temp = ",";
		for (Menu m : menus) {
			temp = temp + m.getCode() + ",";
		}

		System.out.println(temp);
		 
        int i=1;
        String lstrbr="";
		for (Menu m : allMenus) {
			 
			lstrbr=i>1&&i%3==0?"<br>":"";
			if(m.getCode().length()==2&&!m.getCode().endsWith("00"))
			{   
				sb.append("<br>" );
				lstrbr="<br>";
				i=1;
			}
			if (StringUtils.contains(temp, ","+m.getCode()+",")) {
				sb.append("<input type='checkbox' name='" + name + "' value='"
						+ m.getCode() + "' checked='true' />&nbsp;"
						+ m.getCode()+ m.getValue() +","+ "&nbsp;"+lstrbr);
			} else {
				sb.append("<input type='checkbox' name='" + name + "' value='"
						+ m.getCode() + "' />&nbsp;" + m.getCode()+ m.getValue()+"," + "&nbsp;"+lstrbr);
			}
			if(m.getCode().length()>2)
			{i++;}
		}

		return sb.toString();

	}
	
	
}
